/**
 * 
 */
package com.yidin.lsc;

import java.util.HashMap;
import java.util.List;

import com.yidin.entry.PhotoListEntry;
import com.yidin.lsc.VIPCard.VIPCardList;
import com.yidin.util.NetImageBase;
import com.yidin.util.imageCallback;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author cola
 *
 */
public class PhotoAdapter extends BaseAdapter{
	private Context context;
	private View view;
	private List<PhotoListEntry> userInfos;
	private static HashMap<Integer, Bitmap> isSelected;

	public PhotoAdapter(Context context, List<PhotoListEntry> userInfos) {
		this.context = context;
		this.userInfos = userInfos;
		isSelected = new HashMap<Integer, Bitmap>();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return userInfos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return userInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int i = position;
		if (convertView == null) {
			final ImageView iv = new ImageView(context);

			iv.setLayoutParams(new ListView.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			iv.setScaleType(ImageView.ScaleType.FIT_CENTER);// 设置显示比例类型
			ViewHolder vh = new ViewHolder();
			convertView = iv;
			Bitmap bm = null;
			if (this.userInfos.get(position).FilePath != null) {// 请求图标
				bm = new NetImageBase().getLogoBitmap(handler, context,
						this.userInfos.get(position).FilePath,
						new imageCallback() {
							public void getImage(Bitmap bm) {
								// TODO Auto-generated method stub
								if (bm != null) {
									iv.setImageBitmap(bm);
									getIsSelected().put(i, bm);
								}
							}
						});

			}
			if (bm != null) {
				iv.setImageBitmap(bm);
				getIsSelected().put(i, bm);
			}
			vh.itemImage = iv;
			convertView.setTag(vh);
		} else {
			ViewHolder vh = (ViewHolder) convertView.getTag();
			vh.itemImage.setImageBitmap(getIsSelected().get(position));
		}
		return convertView;
	}

	/**
	 * 显示图片
	 */
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 5:{
				NetImageBase item = (NetImageBase) msg.obj;

				Bitmap bm = item.getLocalBitmap(null, context, item.path, 80,
						80);
				item.ic.getImage(bm);
				break;
			}
			case 4:{
				NetImageBase item = (NetImageBase) msg.obj;

				Bitmap bm = item.getLocalBitmap(null, context, item.path, 80,
						80);
				item.ic.getImage(BitmapFactory.decodeResource(context.getResources(), R.drawable.dt_tx));
				break;
			}
			}
		}
	};

	private class ViewHolder {
		public TextView itemame;
		public ImageView itemImage;
	}

	public static HashMap<Integer, Bitmap> getIsSelected() {
		return isSelected;
	}

	private imageCallback imageback;

}
